import java.util.Stack;

public class Sort {

    //直接插入排序
    //稳定
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //希尔排序
    //不稳定
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1){
            gap = gap/3 + 1;
            shell(array,gap);
        }
    }

    private static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0 ; j -= gap) {
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    //选择排序
    //不稳定
    public static void selectSort(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if(array[i] < minIndex){
                    minIndex = array[i];
                }
                if(array[i] > maxIndex){
                    maxIndex = array[i];
                }
            }
            swap(array,left,minIndex);
            //如果最大值是 left下标 那么上面交换完成以后，
            // 最大值跑到了最小值的位置，所以得更新最大值下标
            if(maxIndex == left){
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

    //堆排序
    //不稳定
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length-1;
        while (end >= 0){
            swap(array,0,end);
            siftDown(0,array,end);
            end--;
        }
    }

    private static void createBigHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            siftDown(parent,array, array.length);
        }
    }

    private static void siftDown(int parent,int[] array,int end) {
        int child = 2*parent + 1;
        while (child < end){
            if(child + 1 < end && array[child] < array[child+1]){
                child++;
            }
            if(array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent + 1;
            }else{
                break;
            }
        }
    }

    public static  void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    // 冒泡排序
    //稳定
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean change = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]){
                    swap(array,j,j+1);
                    change = true;
                }
            }
            if(!change){
                break;
            }
        }
    }

    //快速排序
    //不稳定
    public static void quickSort(int[] array) {
        quick(array,0, array.length-1);
    }

    private static void quick(int[] array,int start,int end) {
        if(start >= end){
            return;
        }

        //优化：减少递归次数
        if(end - start + 1 <= 10){
            insertSortRange(array,start,end);
            return;
        }

        //优化：三数取中，使二叉树近似二分递归，减少时间
        int index = midThreeNum(array,start,end);
        swap(array,index,start);

        int par = partition1(array,start,end);

        quick(array,start,par-1);
        quick(array,par+1,end);
    }

    public static void insertSortRange(int[] array,int left,int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    //返回值是中位数的下标
    private static int midThreeNum(int[] array,int left,int right){
        int mid = (left + right)/2;
        if(array[left] < array[right]){
            if(array[mid] < array[left]){
                return left;
            }else if(array[mid] > array[right]){
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid] < array[right]){
                return right;
            }else if(array[mid] > array[left]){
                return left;
            }else{
                return mid;
            }
        }
    }

    //霍尔法
    private static int partition1(int[] array,int left,int right) {
        int i = left;
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            while (left < right && array[left] <= tmp){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    //挖坑法
    private static int partition2(int[] array,int left,int right) {
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){
                right--;
            }
            array[left] = array[right];

            while (left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    //无递归，使用栈实现快速排序；
    public static void quickSortNor(int[] array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int par = partition2(array,left,right);

        if(par > left+1){
            stack.push(left);
            stack.push(par-1);
        }
        if(par < right-1){
            stack.push(par+1);
            stack.push(right);
        }

        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            par = partition2(array,left,right);
            if(par > left+1){
                stack.push(left);
                stack.push(par-1);
            }
            if(par < right-1){
                stack.push(par+1);
                stack.push(right);
            }
        }
    }

    //归并排序
    //稳定
    public static void mergeSort(int[] array){
      mergeSortFun(array,0,array.length-1);
    }

    public static void mergeSortFun(int[] array,int left,int right){
        if(left >= right){
            return;
        }
        int mid = (left + right)/2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);
        //合并
        merge(array,left,mid,right);
    }

    private static void merge(int[] array,int left,int mid,int right){
        int[] tmp = new int[right-left+1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;

        while (s1 <= e1 && s2 <= e2){
            if(array[s1] <= array[s2]){
                tmp[k++] = array[s1++];
            }else{
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2){
            tmp[k++] = array[s2++];
        }
        //走到这里 相当于tmp数组 当中 所有的元素 都有序了
        //接下来把tmp数组的内容 拷贝到array数组当中
        for (int i = 0; i < k; i++) {
           array[i+left] = tmp[i];
        }
    }

    //非递归实现归并排序
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap < array.length){
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length){
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length){
                    right = array.length-1;
                }

                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }

    //计数排序（不需要比较大小）
    public static void countSort(int[] array){
        //1.遍历找最大最小值;
        int maxVal = array[0];
        int minVal = array[0];
        for (int i = 0; i < array.length; i++) {
            if(maxVal < array[i]){
                maxVal = array[i];
            }
            if(minVal > array[i]){
                minVal = array[i];
            }
        }
        //2.定义count数组
        int[] count = new int[maxVal - minVal + 1];
        //3.遍历array数组，把值放入计数数组中：
        for (int i = 0; i < array.length; i++) {
            int val = array[i];
            count[val - minVal]++;
        }
        //4.遍历计数数组;
        int index = 0;//array下标;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0){
                array[index] = i + minVal;
                index++;
                count[i]--;
            }
        }
    }

}
